#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import yagmail
import zmail
from send_qq_mail import send_mail
import datetime
import random

class MailHandle:

    def get_latest_mail(config):
        server = zmail.server(config.user,config.password)
        mail = server.get_latest()
        zmail.show(mail)
        print(mail['subject'],mail['from'],mail['date'],sep='\n')

        tmails = server.get_mails(subject='测试邮件的标题')
        len(tmails)
        for t in tmails:
            print(t['subject'],t['from'])

    def send_mail(config, subject, contents):
        yag = yagmail.SMTP(user=config.user, password=config.password, host=config.host)
        yag.send(to=config.to, subject=subject, contents=contents)

    def send_mail_by_html(config, subject, contents):
        yag = yagmail.SMTP(user=config.user, password=config.password, host=config.host)
        subject = '测试邮件的标题【HTML】'
        body = '测试邮件的内容部分,看下方，看下方，看下方'
        h1 = '<h1>Spbeen</h1>'
        a_link = '点击<a href="http://www.spbeen.com">链接</a>，前往Spbeen网站'
        py_source = "背影.docx"
        yag.send(to = config.to,subject = subject,contents = [body, h1, a_link,py_source])

    def regular_send_mail(url):
        with open(url,'a',encoding='utf8') as file:
            random_num = random.randint(1,10000)
            current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            file.writelines("当前时间：{} 随机数值：{}\n".format(current_time, random_num))
            subject = "定期任务执行完成"
            contents = "当前时间：{} 随机数值：{}\n".format(current_time, random_num)
            send_mail(subject,contents)

class MailConfig:
    type = 'qq mail'
    user = 'givemefive94@qq.com'
    password = '***********************'
    host = 'smtp.qq.com'
    to = 'givemefive94@qq.com'
    subject = '测试邮件的标题【HTML】'
    body = '测试邮件的内容部分,看下方，看下方，看下方'
    h1 = '<h1>Spbeen</h1>'
    a_link = '点击<a href="http://www.spbeen.com">链接</a>，前往Spbeen网站'
    py_source = "背影.docx"